set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)

cmake_minimum_required(VERSION 3.27)

project(GD32F470FreeRTOS C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

set(PATH "D:/gcc-arm-none-eabi/bin")
set(EXECUTABLE_SUFFIX ".exe")

# specify cross-compilers and tools
set(CMAKE_C_COMPILER ${PATH}/arm-none-eabi-gcc${EXECUTABLE_SUFFIX})
set(CMAKE_CXX_COMPILER ${PATH}/arm-none-eabi-g++${EXECUTABLE_SUFFIX})
set(CMAKE_ASM_COMPILER  ${PATH}/arm-none-eabi-gcc${EXECUTABLE_SUFFIX})
set(CMAKE_AR ${PATH}/arm-none-eabi-ar${EXECUTABLE_SUFFIX})
set(CMAKE_OBJCOPY ${PATH}/arm-none-eabi-objcopy${EXECUTABLE_SUFFIX})
set(CMAKE_OBJDUMP ${PATH}/arm-none-eabi-objdump${EXECUTABLE_SUFFIX})
set(SIZE ${PATH}/arm-none-eabi-size${EXECUTABLE_SUFFIX})
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

#Uncomment for hardware floating point
add_compile_definitions(ARM_MATH_CM4;ARM_MATH_MATRIX_CHECK;ARM_MATH_ROUNDING)
add_compile_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16)
add_link_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16)

#Uncomment for software floating point
#add_compile_options(-mfloat-abi=soft)

add_compile_options(-mcpu=cortex-m4 -mthumb -mthumb-interwork)
add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0)

# uncomment to mitigate c++17 absolute addresses warnings
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-register")

# Enable assembler files preprocessing
add_compile_options($<$<COMPILE_LANGUAGE:ASM>:-x$<SEMICOLON>assembler-with-cpp>)

if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
    message(STATUS "Maximum optimization for speed")
    add_compile_options(-Ofast)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")
    message(STATUS "Maximum optimization for speed, debug info included")
    add_compile_options(-Ofast -g)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel")
    message(STATUS "Maximum optimization for size")
    add_compile_options(-Os)
else ()
    message(STATUS "Minimal optimization, debug info included")
    add_compile_options(-Og -g)
endif ()

include_directories(Firmware/GD32F4xx_standard_peripheral/Include
        Firmware/CMSIS/Include
        Firmware/CMSIS/GD/GD32F4xx/Include
        Firmware/CMSIS
        User
        User/Drive/inc
        Firmware/FreeRTOS/include
        Firmware/FreeRTOS/portable/GCC/ARM_CM4F
        Firmware/Fatfs/source
        Firmware/LVGL
)

add_definitions(-DUSE_STDPERIPH_DRIVER -DGD32F470)

file(GLOB GD32LIB_SOURCES "Firmware/GD32F4xx_standard_peripheral/Source/*.c")

set(CMSIS_SOURCES Firmware/CMSIS/GD/GD32F4xx/Source/system_gd32f4xx.c)

set(USER_SOURCES
    User/gd32f4xx_it.c
    User/main.c
    User/Drive/src/delay.c
    User/Drive/src/Usart.c
    User/Drive/src/SdRam.c
    User/Drive/src/sdcard.c
    User/Drive/src/MySPI.c
    User/Drive/src/SPILCD.c
    User/Drive/src/test.c
    User/Drive/src/GUI.c
)
set(FREERTOS_SOURCES
    Firmware/FreeRTOS/croutine.c
    Firmware/FreeRTOS/event_groups.c
    Firmware/FreeRTOS/list.c
    Firmware/FreeRTOS/queue.c
    Firmware/FreeRTOS/stream_buffer.c
    Firmware/FreeRTOS/tasks.c
    Firmware/FreeRTOS/timers.c
    Firmware/FreeRTOS/portable/GCC/ARM_CM4F/port.c
    Firmware/FreeRTOS/portable/MemMang/heap_5.c
)

set(FATFS_SOURCES
    Firmware/Fatfs/source/ff.c
    Firmware/Fatfs/source/ffsystem.c
    Firmware/Fatfs/source/diskio.c
    Firmware/Fatfs/source/ffunicode.c
    Firmware/Fatfs/source/drive.c
)

set(LVGL_SOURCES
        #[[Firmware/LVGL/examples/porting/lv_port_disp.c
        Firmware/LVGL/src/core/lv_obj_event.c
        Firmware/LVGL/src/core/lv_group.c
        Firmware/LVGL/src/core/lv_obj_draw.c
        Firmware/LVGL/src/core/lv_obj_id_builtin.c
        Firmware/LVGL/src/core/lv_obj_class.c
        Firmware/LVGL/src/core/lv_obj_pos.c
        Firmware/LVGL/src/core/lv_obj.c
        Firmware/LVGL/src/core/lv_obj_property.c
        Firmware/LVGL/src/core/lv_obj_scroll.c
        Firmware/LVGL/src/core/lv_obj_style.c
        Firmware/LVGL/src/core/lv_obj_style_gen.c
        Firmware/LVGL/src/core/lv_obj_tree.c
        Firmware/LVGL/src/core/lv_refr.c
        Firmware/LVGL/src/font/lv_font_montserrat_44.c
        Firmware/LVGL/src/font/lv_font_montserrat_28.c
        Firmware/LVGL/src/font/lv_font_montserrat_16.c
        Firmware/LVGL/src/font/lv_font_montserrat_48.c
        Firmware/LVGL/src/font/lv_font_montserrat_38.c
        Firmware/LVGL/src/font/lv_font_montserrat_8.c
        Firmware/LVGL/src/font/lv_font_montserrat_12.c
        Firmware/LVGL/src/font/lv_font_unscii_16.c
        Firmware/LVGL/src/font/lv_font_unscii_8.c
        Firmware/LVGL/src/font/lv_binfont_loader.c
        Firmware/LVGL/src/font/lv_font_montserrat_46.c
        Firmware/LVGL/src/font/lv_font_montserrat_26.c
        Firmware/LVGL/src/font/lv_font_simsun_16_cjk.c
        Firmware/LVGL/src/font/lv_font_montserrat_32.c
        Firmware/LVGL/src/font/lv_font_montserrat_24.c
        Firmware/LVGL/src/font/lv_font_dejavu_16_persian_hebrew.c
        Firmware/LVGL/src/font/lv_font_montserrat_40.c
        Firmware/LVGL/src/font/lv_font_montserrat_22.c
        Firmware/LVGL/src/font/lv_font_fmt_txt.c
        Firmware/LVGL/src/font/lv_font_montserrat_10.c
        Firmware/LVGL/src/font/lv_font_montserrat_20.c
        Firmware/LVGL/src/font/lv_font_montserrat_18.c
        Firmware/LVGL/src/font/lv_font_montserrat_14.c
        Firmware/LVGL/src/display/lv_display.c
        Firmware/LVGL/src/font/lv_font_montserrat_34.c
        Firmware/LVGL/src/font/lv_font_montserrat_28_compressed.c
        Firmware/LVGL/src/font/lv_font_montserrat_30.c
        Firmware/LVGL/src/font/lv_font.c
        Firmware/LVGL/src/font/lv_font_montserrat_42.c
        Firmware/LVGL/src/font/lv_font_montserrat_36.c]]
)
add_subdirectory(Firmware/LVGL)

set(SOURCES ${GD32LIB_SOURCES} ${USER_SOURCES} ${FREERTOS_SOURCES} ${FATFS_SOURCES} ${LVGL_SOURCES})

set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/Firmware/Ld/GD32F470ZETX_FALSH.ld)

add_link_options(-Wl,-gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map)
add_link_options(-mcpu=cortex-m4 -mthumb -mthumb-interwork)
add_link_options(-T ${LINKER_SCRIPT})

add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT})

set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)

add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
        COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
        COMMENT "Building ${HEX_FILE}
Building ${BIN_FILE}")
